knitr::opts_chunk$set(warning=FALSE,message = FALSE)
A word cloud is a very simple yet effective visualization for any form of text analytics. Rather than showing a bar graph of certain keywords it is much more emphatic to visualize them in a word cloud.
For this purpose I have laid down steps to create a basic word cloud in as few lines as possible.
In R we can use package wordcloud to create basic wordclouds:
library(wordcloud)
As we do not have a word list we can create one
word = c("These","are","some","random","words","for","a","simple","word","cloud")
We also need a frequency list or any other metric on which we might need to emphasize
freq = c(1,10,20,2,30,15,70,50,45,80)
A wordcloud can be generate as
set.seed(1234) #set seed to obtain the same arrangement of words each time
wordcloud(words = word,freq = freq)
You can explore many other options like maximum number of words to be plotted, the minimum frequency required to be plotted etc.
However you might have seen fancy wordclouds which are masked on an image or a piece of text. To visualize such wordclouds R offers a wordcloud2 package
Let us try to create a wordcloud over an image. The background image is referred to as a mask. After trial an wrror I have noticed that a single solid colour mask image works best when trying to visualize such a word cloud
As its Halloween season, we can try to project a word cloud on a witch. This is an image I got from google search (I do not own it and the rights belong to respective owners):
Witch Mask Image for wordcloud
I have loaded a dataset with 1000 keywords and the number of impressions. We can start by loading the library wordcloud2
library(wordcloud2)
A simple wordcloud can be generated by supplying a dataframe with the word and frequency column
library(dplyr)
setwd("D:/Google Drive/Fall 2017/MSBA 6410 Exploratory/Week 8/Assignment 4/HW 4")
df = read.csv("keywords.csv")
temp = df %>% select(query,num_impressions)
wordcloud2(data = temp)
We can pass the mask (with a relative path if needed) in the figpath argument
wordcloud2(data = temp,figPath = "D:/Google Drive/Fall 2017/MSBA 6410 Exploratory/Week 8/Assignment 4/HW 4/wcWitch.png")
wc without size and colour
But this looks nothing like the witch, we can pass a size argument to scale it, also we can supply a color, I have set it to orange to be consistent with halloween
wordcloud2(data = temp,figPath = "D:/Google Drive/Fall 2017/MSBA 6410 Exploratory/Week 8/Assignment 4/HW 4/wcWitch.png",color = "orange",size=5)
wc final